package com.xy.etl.ibatis.node.impl;

import com.xy.etl.ibatis.core.XySQLExePara;
import com.xy.etl.ibatis.core.XySQLKit;
import com.xy.etl.ibatis.core.XySQLTaskContext;
import com.xy.etl.ibatis.fragment.IXySQLFragment;
import com.xy.etl.ibatis.node.XySQLSuperNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 最底层节点，他包含SQL片段
 */
public class XySQLStringNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return null;
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLStringNode();
    }


    //片段
    private List<IXySQLFragment> fragmentList;

    /**
     * 设定内容
     *
     * @param txt
     */
    public void setTxt(String txt) {
        if (txt != null) {
            super.setTxt(" " + txt + " ");
        }
    }


    /**
     * 分析字符串对象
     *
     * @return
     * @throws Exception
     */
    public void parse() throws Exception {
        //分析SQL片段，以后执行的就是这些片段
        fragmentList = XySQLKit.processFragment(this.getTxt());
    }

    /**
     * 绑定SQL
     *
     * @param task
     * @return
     * @throws Exception
     */
    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {
        List<XySQLExePara> list = new ArrayList<>();
        List<XySQLExePara> listTemp;
        if (fragmentList != null) {
            for (IXySQLFragment fo : fragmentList) {
                listTemp = fo.boundSql(task);
                if (listTemp != null && listTemp.size() > 0) {
                    list.addAll(listTemp);
                }
            }
        }
        return list;
    }

}
